﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Frame;
using System.IO;

namespace ApplicationManagement
{
    public partial class MainFrm : Plugin
    {

        private  Global m_global;
        private List<bool> dgvShow;
        public MainFrm(FrameApplication app):base(app)
        {
            InitializeComponent();
            m_app.InitialUiEvnet += InitUI;
            m_global = new Global(m_app);
            this.m_InitShow = true;
            this.m_inToolBar = true; ;//是否显示在工具栏上
            this.Enabled = true;   //窗体是否有效
            this.FloatSize = this.Size; //悬浮显示的窗口大小就是现在的大小
            this.OnMenu = "ApplicationManagement";
            this.toolBarIcon = Properties.Resources.应用程序1;
            this.Text = "应用程序";
            dgApp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            
            dgvShow = new List<bool>();

        }

        public override void InitUI()
        {
            dgApp.Rows.Clear();
            foreach (AppClass appClass in m_app.ApplicationManager.Application)
            {
                dgApp.Rows.Add();
                int row = dgApp.RowCount - 1;
                dgApp.Rows[row].Cells[0].Value = appClass.Expand ? "-" : "+";
                if (File.Exists(m_global.get_full_address(appClass.image)));
                    dgApp.Rows[row].Cells[1].Value = Image.FromFile(m_global.get_full_address(appClass.image));
                dgApp.Rows[row].Cells[2].Value = appClass.Name;
                foreach(App app in appClass.AppList)
                {
                    dgApp.Rows.Add();
                    int _row = dgApp.RowCount - 1;
                    dgApp.Rows[_row].Cells[0].Value = string.Empty;
                    if(File.Exists(app.Path))
                        dgApp.Rows[_row].Cells[1].Value = Icon.ExtractAssociatedIcon(app.Path);
                    dgApp.Rows[_row].Cells[2].Value = app.Name;
                    dgApp.Rows[_row].Visible = appClass.Expand;
                }
            }
            if(m_app.ApplicationManager.Application.Count == 0)
            {
                dgApp.Rows.Add();
                int row = dgApp.RowCount - 1;
                dgApp.Rows[row].Cells[0].Value =  "+";
                dgApp.Rows[row].Cells[2].Value = string.Empty;
            }
        }

        private void dgApp_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 0)
            {
                dgApp_CellContentDoubleClick(sender, e); 
            }
        }

        private void dgApp_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;            
            string _v = dgApp.Rows[rowindex].Cells[0].Value.ToString();
            string name = dgApp.Rows[rowindex].Cells[2].Value.ToString();
            if (string.Empty == _v)
            {
                tsMenuItemRun_Click(null, null);
            }
            else
            {
                AppClass appCLass = m_app.ApplicationManager.GetClass(name);
                int appCount = m_app.ApplicationManager.GetAppList(name).Count;
                appCLass.Expand = _v.CompareTo("+")==0;
                dgvHideRows(rowindex, appCount, appCLass.Expand);
            }

        }

        private void dgvHideRows(int index, int count, bool isShow = true)
        {

            dgApp.Rows[index].Cells[0].Value = isShow ? "-" : "+";
            for (int i = index+1; i <= index + count; i++)
            {
                dgApp.Rows[i].Visible = isShow;
                
            }
        }

        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //m_app.ApplicationManager.SaveXml();
        }

        private void dgApp_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowIndex = e.RowIndex;
            if (e.Button == MouseButtons.Right)
            {
                cMenu.Items.Clear();                
                switch (e.ColumnIndex)
                {
                    case 2:
                        if (dgApp.Rows[rowIndex].Cells[0].Value.ToString() == string.Empty)
                        {
                            App app = m_app.ApplicationManager.GetApp(dgApp.Rows[rowIndex].Cells[2].Value.ToString());
                            cMenu.Items.Add("运行", Properties.Resources.运行, tsMenuItemRun_Click);
                            cMenu.Items.Add("收藏", app.Favorite ? Properties.Resources.收藏 : Properties.Resources.未收藏, tsMenufavorite_Click);
                        }
                        cMenu.Items.Add("添加", Properties.Resources.添加.ToBitmap(), tsMenuAdd_Click);
                        cMenu.Items.Add("删除", Properties.Resources.删除, tsMenuDel_Click);
                        cMenu.Items.Add("属性", Properties.Resources.属性, tsMenuProperty_Click);
                        break;
                }
                cMenu.Items.Add("全部展开", Properties.Resources.展开, tsMenuItemExpandAll_Click);
                cMenu.Items.Add("全部折叠", Properties.Resources.折叠, tsMenuItemFoldAll_Click);
            }
            
        }

        private void tsMenufavorite_Click(object sender, EventArgs e)
        {
            ToolStripItem item = (ToolStripItem)sender;
            int row = dgApp.SelectedCells[0].RowIndex;
            string _v = dgApp.Rows[row].Cells[2].Value.ToString();
            App app = m_app.ApplicationManager.GetApp(_v);
            app.Favorite = !app.Favorite;
            item.Image = app.Favorite ? Properties.Resources.收藏 : Properties.Resources.未收藏;

        }

        private void tsMenuItemExpandAll_Click(object sender, EventArgs e)
        {
            ExpandOrFold(true);
        }

        private void tsMenuItemFoldAll_Click(object sender, EventArgs e)
        {
            ExpandOrFold(false);
        }

        private void ExpandOrFold(bool expand)
        {
            int rows = dgApp.RowCount;
            int index = 0;
            foreach(AppClass appClass in m_app.ApplicationManager.Application)
            {
                int count = appClass.AppList.Count;
                dgvHideRows(index, count, expand); 
                index += count+1;
            }
        }

        private void tsMenuAdd_Click(object sender, EventArgs e)
        {
            
            AddTypeFrm _frm = new AddTypeFrm();
            int type = (int)_frm.ShowDialog(this);

            Add frm = null;
            int row = dgApp.SelectedCells[0].RowIndex;
            string name = dgApp.Rows[row].Cells[2].Value.ToString();
            if (type == 2)
            {
                AppClass appClass = m_app.ApplicationManager.AppToClass(name);
                if (appClass == null)
                    appClass = m_app.ApplicationManager.GetClass(name);
                frm = new Add(appClass);              
            }
            else
            {
                frm = new Add();
            }
            frm.global = m_global;
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                InitUI();
            }

        }

        private void tsMenuItemRun_Click(object sender, EventArgs e)
        {
            int row = dgApp.SelectedCells[0].RowIndex;
            string _v = dgApp.Rows[row].Cells[0].Value.ToString();
            if (_v == string.Empty)
            {
                m_app.Log.AddErrInfoLog(m_app.ApplicationManager.RunApp(dgApp.Rows[row].Cells[2].Value.ToString()), "运动应用程序:"+ dgApp.Rows[row].Cells[2].Value.ToString());
            }
        }


        private void tsMenuDel_Click(object sender, EventArgs e)
        {
            int row = dgApp.SelectedCells[0].RowIndex;
            string _v = dgApp.Rows[row].Cells[0].Value.ToString();
            
            if (_v == string.Empty)
            {
                AppClass appClass = m_app.ApplicationManager.AppToClass(dgApp.Rows[row].Cells[2].Value.ToString());
                App app = m_app.ApplicationManager.GetApp(dgApp.Rows[row].Cells[2].Value.ToString());
                appClass.AppList.Remove(app);
            }
            else
            {
                AppClass appClass = m_app.ApplicationManager.GetClass(dgApp.Rows[row].Cells[2].Value.ToString());
                m_app.ApplicationManager.Application.Remove(appClass);
            }
            InitUI();
        }

        private void tsMenuProperty_Click(object sender, EventArgs e)
        {
            Property frm = null;
            int row = dgApp.SelectedCells[0].RowIndex;
            string _v = dgApp.Rows[row].Cells[0].Value.ToString();
            if (_v == string.Empty)
            {
                AppClass appClass = m_app.ApplicationManager.AppToClass(dgApp.Rows[row].Cells[2].Value.ToString());
                App app = m_app.ApplicationManager.GetApp(dgApp.Rows[row].Cells[2].Value.ToString());
                frm = new Property(app);
            }
            else
            {
                AppClass appClass = m_app.ApplicationManager.GetClass(dgApp.Rows[row].Cells[2].Value.ToString());
                frm = new Property(appClass);
            }
            frm.global = m_global;
            if(frm.ShowDialog() == DialogResult.OK)
            {
                InitUI();
            }
        }
    }
}
